//
//  LSZThemesAlbumsVC.m
//  JKFuYinProject
//
//  Created by 九酷 on 2017/9/21.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "LSZThemesAlbumsVC.h"
#import "LSZThemesAlbumsVC+LCAVPlay.h"

@interface LSZThemesAlbumsVC ()

@end

@implementation LSZThemesAlbumsVC
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.numberPage = 1;
    [self QueryTheCachePastorIsSermonOnTheDataOfTopic :self.numberPage];
    [LCFloatingWindow sharedInstance].hidden = NO;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    [LCFloatingWindow sharedInstance].hidden = YES;
}
#pragma mark - 查看是否有缓存
-(void)QueryTheCachePastorIsSermonOnTheDataOfTopic :(NSInteger)number
{
    __weak typeof(self) weakSelf = self;
    NSString * StrFile = [NSString stringWithFormat:@"%@_%@_%@",jiangdao_yinpin_play,self.titleStr,weakSelf.parentId];
    [LSZCacheHandling initWithfindPlist:StrFile :^(BOOL isOk, NSDictionary * dic) {
        if (isOk == YES) {
            LSZPriestPreachedAudioManager * MG  = [[LSZPriestPreachedAudioManager alloc]initWithDicPriestPreachedAudioManager:dic type:self.typeStr value:@"3"];
            [weakSelf.mutArrayData removeAllObjects];
            [weakSelf.mutArrayData addObjectsFromArray:MG.mutDataArray];            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [self.tableViewAlbums reloadData];
            }];
        }else{
            [self ThemesAlbumsData :number];
        }
    }];
}
#pragma mark  - 请求数据
-(void)ThemesAlbumsData :(NSInteger)number
{
     __weak typeof(self) weakSelf = self;
    [LSZRequestResolutionDic initWithThemeAlbum:[NSString stringWithFormat:@"%ld",(long)number] type:weakSelf.typeStr opt:weakSelf.optStr parentId:weakSelf.parentId value:@"3" titleStr:self.titleStr :^(BOOL isOk, NSString * str, LSZPriestPreachedAudioManager * AudioManager) {
        if (isOk == YES) {
            if (number == 1) {
                [weakSelf.mutArrayData removeAllObjects];
                [weakSelf.mutArrayData addObjectsFromArray:AudioManager.mutDataArray];
            }else{
                [weakSelf.mutArrayData addObjectsFromArray:AudioManager.mutDataArray];
            }
            [weakSelf.tableViewAlbums  reloadData];
        }else{
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",str]];
            [SVProgressHUD dismissWithDelay:1];
        }
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creareAVPlay];
//    LCPlaylist *playlistTest = _PlaylistMgr.allPlaylists.firstObject;
//    _LCPlayer.songs = playlistTest.allSongs;
    self.title = [NSString stringWithFormat:@"%@",self.titleStr];
    self.mutArrayData = [[NSMutableArray alloc]init];
    [self.tableViewAlbums registerClass:[LSZAudioFrequencyTableViewCell class] forCellReuseIdentifier:@"LSZAudioFrequencyTableViewCell"];
    self.tableViewAlbums.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 进入刷新状态后会自动调用这个block
            self.numberPage = 1;
            //赞美诗的分类
            [self QueryTheCachePastorIsSermonOnTheDataOfTopic:self.numberPage];
            [self.tableViewAlbums.mj_header endRefreshing];
        });
    }];
    // 上拉刷新
    self.tableViewAlbums.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 进入刷新状态后会自动调用这个block
            self.numberPage++;
            //赞美诗的分类
            [self ThemesAlbumsData:self.numberPage];
            [self.tableViewAlbums.mj_footer endRefreshing];
        });
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  self.mutArrayData.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZAudioFrequencyTableViewCell * cell = [[LSZAudioFrequencyTableViewCell alloc]initWithNumberAudioFrequency:1];
    LSZPriestPreachedAudioMg * audioMg = nil;
    audioMg = self.mutArrayData[indexPath.row];
    cell.labelNumber.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row+1];
    [cell.imageViewAudio                                 sd_setImageWithURL:[NSURL URLWithString:audioMg.image] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    cell.imageViewHook.image = [UIImage imageNamed:@"placeholder"];
    cell.labelTitle.text = [NSString stringWithFormat:@"%@",audioMg.title];
    cell.labelChapters.text = [NSString stringWithFormat:@"%@个专辑",audioMg.total_music];
    if ([self.lastClassNumberRow isEqualToString:@"2"]) {
        cell.butShare.hidden = NO;
    }else{
        cell.butShare.hidden = YES;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZPriestPreachedAudioMg * audioMg = nil;
    audioMg = self.mutArrayData[indexPath.row];
    LSZPlayThemeVC * vc = [[LSZPlayThemeVC alloc]init];
    vc.titleStr = audioMg.title;
    vc.typeStr = self.typeStr;
    vc.optStr = @"3";
    vc.parentId = audioMg.id_id;
    vc.lastClassNumberRow = self.lastClassNumberRow;
    vc.lastPageNumberRow = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}
-(UITableView *)tableViewAlbums
{
    if (_tableViewAlbums == nil) {
        _tableViewAlbums = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-60-HOME_INDICATOR_HEIGHT) style:UITableViewStylePlain];
        _tableViewAlbums.delegate = self;
        _tableViewAlbums.dataSource = self;
        //添加以下代码关闭估算行高
        _tableViewAlbums.estimatedRowHeight = 0;
        _tableViewAlbums.estimatedSectionHeaderHeight = 0;
        _tableViewAlbums.estimatedSectionFooterHeight = 0;
        [self.view addSubview:_tableViewAlbums];
    }
    return _tableViewAlbums;
}
@end
